/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.controller;

import com.tesam.gedeon.entity.Country;
import com.tesam.gedeon.entity.CountryProvider;
import com.tesam.gedeon.entity.Provider;
import com.tesam.gedeon.service.CountryProviderService;
import com.tesam.gedeon.service.CountryService;
import com.tesam.gedeon.service.ProviderService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Sistema
 */
@Controller
public class CountryProviderController {
    
    @Autowired
    SystemUserController system;
    @Autowired
    CountryProviderService countryProviderService ;
    @Autowired
    CountryService countryService;        
    @Autowired
    ProviderService providerService ;        
    
    Country country = new Country();
    Provider provider = new Provider();
    
    @RequestMapping(value ="/mediationcountryprovider/index")
    public ModelAndView index(HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationcountryprovider/index");
        
        system.loadDataSystem(mv, request);
        
        return mv;
    }
    
    @RequestMapping(value = "/mediationcountryprovider/new")
    public ModelAndView newCountryProvider(HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationcountryprovider/new");
        
        system.loadDataSystem(mv, request);
        
        mv.addObject("country", countryService.getListAll());
        mv.addObject("provider", providerService.getAll());
        
        return mv;
    }
    
    @RequestMapping(value = "/mediationcountryprovider/delete")
    public ModelAndView delete(@ModelAttribute("CountryProvider")CountryProvider countryProvider ,HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationcountryprovider/delete");
        
        countryProviderService.delete(countryProvider);
        
        return mv;
    }
    
    @RequestMapping(value = "/mediationcountryprovider/update")
    public ModelAndView update(@ModelAttribute("CountryProvider")CountryProvider countryProvider ,HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationcountryprovider/update");
        
        system.loadDataSystem(mv, request);
        
        mv.addObject("country", countryService.getListAll());
        mv.addObject("provider", providerService.getAll());
        mv.addObject("countryProvider", countryProviderService.getCountryProviderById(countryProvider));
        
        return mv;
    }
    
    @RequestMapping(value = "/mediationcountryprovider/saveupdate")
    public ModelAndView saveUpdate(@ModelAttribute("CountryProvider")CountryProvider countryProvider ,HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationcountryprovider/save");
        
        system.loadDataSystem(mv, request);
        
        country.setId(Integer.parseInt(request.getParameter("CboCountry").toString()));
        
        provider.setId(Integer.parseInt(request.getParameter("CboProvider").toString()));
        
        countryProvider.setCountry(countryService.getCountryById(country).get(0));
        countryProvider.setProvider(providerService.getById(provider.getId()));
        
        countryProviderService.update(countryProvider);
        
        mv.addObject("id", countryProvider.getId());
        
        return mv;
    }
    
    @RequestMapping(value = "/mediationcountryprovider/newsave")
    public ModelAndView newSave(@ModelAttribute("CountryProvider")CountryProvider countryProvider ,HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationcountryprovider/save");
        
        country.setId(Integer.parseInt(request.getParameter("CboCountry").toString()));
        
        provider.setId(Integer.parseInt(request.getParameter("CboProvider").toString()));
        
        countryProvider.setCountry(countryService.getCountryById(country).get(0));
        countryProvider.setProvider(providerService.getById(provider.getId()));
        system.loadDataSystem(mv, request);
        
        mv.addObject("id", countryProviderService.save(countryProvider));
        
        return mv;
    }
    
    @RequestMapping(value = "/mediationcountryprovider/view")
    public ModelAndView view(@ModelAttribute("CountryProvider")CountryProvider countryProvider , HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationcountryprovider/view");
        
        system.loadDataSystem(mv, request);
        
        mv.addObject("countryProvider", countryProviderService.getCountryProviderById(countryProvider));
        
        return mv ;
    }
    
       @RequestMapping(value = "/mediationcountryprovider/ajax" , method = RequestMethod.GET)
    public @ResponseBody
    String doAjaxGet() {

        int cantidadRegistros = 0;
        try {
            List<CountryProvider> list = countryProviderService.getCountryProviderByLimit("cp.name", "asc",0, 20);
 
            JSONObject json = new JSONObject();
            JSONArray array = new JSONArray();

            if (list.size() > 0) {
                for (CountryProvider countryProvider : list) {
                    JSONArray arrayPut = new JSONArray(); 
                    arrayPut.put(countryProvider.getCountry().getName()); 
                    arrayPut.put(countryProvider.getCountry().getCode()); 
                    arrayPut.put(countryProvider.getProvider().getName()); 
                    array.put(arrayPut); 
                }
            } 
            
            cantidadRegistros = countryProviderService.getCountAll();
            json.put("iTotalRecords", cantidadRegistros);
            json.put("iTotalDisplayRecords", cantidadRegistros);
            json.put("aaData", array);

            return json.toString();
        } catch (Exception e) {
            System.out.print("Error - Controller-doAjax:" + e.getMessage());
        }

        return null;
    }
     
      @RequestMapping(value = "/mediationcountryprovider/ajax", method = RequestMethod.POST)
    public @ResponseBody
    String doAjaxPost(HttpServletRequest request) {

        List<CountryProvider> listCountryProvider = null;
        int cantidadRegistros = 0;

        String[] columns = {"cp.countrybillingcode", "cp.name", "pr.code" , "pr.name"  , "pr.name"};

        String startLimit = request.getParameter("iDisplayStart");
        String endLimit = request.getParameter("iDisplayLength");
        String dataToSort = columns[Integer.valueOf(request
                .getParameter("iSortCol_0"))];
        String directionOfOrder = request.getParameter("sSortDir_0");
        String textSearch = request.getParameter("sSearch").toUpperCase();

        int _startLimit = 0;
        int _endLimit = 0;

        if (startLimit != null && !startLimit.equals("0")) {

            if (!startLimit.trim().isEmpty()) {
                _startLimit = Integer.valueOf(startLimit);
            }
        }

        if (endLimit != null && !endLimit.equals("0")) {
            if (!endLimit.trim().isEmpty()) {
                _endLimit = Integer.valueOf(endLimit);
            }
        }

        if (textSearch != null) {
            if (!textSearch.trim().isEmpty()) {
                textSearch = textSearch.trim();
                CountryProvider countryProvider = new CountryProvider();
                countryProvider.setCountryProviderCode(textSearch);
                listCountryProvider = countryProviderService.getCountryProviderBySearch(dataToSort, directionOfOrder ,_startLimit,_endLimit, countryProvider);
                cantidadRegistros = countryProviderService.getCountBySearch(countryProvider);
                return countryProviderService.getJsonDataFromArrayList(listCountryProvider, cantidadRegistros);

            }
        }

        listCountryProvider = countryProviderService.getCountryProviderByLimit(dataToSort, directionOfOrder ,_startLimit,
                (_endLimit));

        cantidadRegistros = countryProviderService.getCountAll();

        return countryProviderService.getJsonDataFromArrayList(listCountryProvider, cantidadRegistros);
    }

    
}
